package org.dialMsg.Server.ServerImp;

import org.dialMsg.Mapper.TDataSourceBatchMapper;
import org.dialMsg.Pojo.DataSourceBatchDetail;
import org.dialMsg.Pojo.RobotExecLog;
import org.dialMsg.Pojo.TDataSourceBatch;
import org.dialMsg.Pojo.TDialResult;
import org.dialMsg.Server.ServerInt.RobotExecLogService;
import org.dialMsg.Server.ServerInt.TDataSourceBatchService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * @author DPF
 */
@Service
public class TDataSourceBatchServiceImpl implements TDataSourceBatchService {

    @Autowired
    private TDataSourceBatchMapper dataSourceBatchMapper;
    @Autowired
    private RobotExecLogService robotExecLogService;

    @Transactional
    @Override
    public void insertBatchWithDetails(TDataSourceBatch tDataSourceBatch) {

        // 插入主数据
        dataSourceBatchMapper.insertTDataSourceBatch(tDataSourceBatch);
        List<DataSourceBatchDetail> dataSourceBatchDetails = tDataSourceBatch.getDataSourceBatchDetails();
        ArrayList<TDialResult> tDialResults = new ArrayList<>();
        // 遍历每个 DataSourceBatchDetail 对象
        for (DataSourceBatchDetail dataSourceBatchDetail : dataSourceBatchDetails) {
            List<TDialResult> detailDialResults = dataSourceBatchDetail.getTDialResults();
            if (detailDialResults != null) {
                tDialResults.addAll(detailDialResults);
            }
            for (TDialResult tDialResult : tDialResults) {
                if (tDialResult.getCallType() == 1) {
                    tDialResult.setDurationOfRinging(0);
                    tDialResult.setDurationOfSeatRing(0);
                    tDialResult.setDurationOfSeatConversing(0);
                    tDialResult.setBridgeResult(null);
                    tDialResult.setHumanStatus(10);
                } else if (tDialResult.getBridgeResult() != 3) {
                    tDialResult.setDurationOfSeatConversing(0);
                }
                tDialResult.setShowName(tDataSourceBatch.getShowName());
                tDialResult.setAccountId(tDataSourceBatch.getCompanyUserId());
                tDialResult.setBusinessId(tDataSourceBatch.getBusinessId());
                tDialResult.setDataSourceBatchId(tDataSourceBatch.getId());
            }

        }
        // 插入详情数据
        dataSourceBatchMapper.insertDataSourceBatchDetails(tDataSourceBatch, dataSourceBatchDetails);
        // 插入拨号结果
        if (!dataSourceBatchDetails.isEmpty()) {
            for (DataSourceBatchDetail dataSourceBatchDetail : dataSourceBatchDetails) {
                List<TDialResult> detailDialResults = dataSourceBatchDetail.getTDialResults();
                if (detailDialResults != null && !detailDialResults.isEmpty()) {
                    dataSourceBatchMapper.insetDialResults(dataSourceBatchDetail, detailDialResults);
                }
            }
        }

    }

    @Override
    public TDataSourceBatch selectById(Integer id) {
        return dataSourceBatchMapper.selectById(id);
    }

    @Override
    public List<TDataSourceBatch> queryBySize(Integer size) {
        List<TDataSourceBatch> tDataSourceBatches = dataSourceBatchMapper.queryBySize(size);
        return tDataSourceBatches == null ? new ArrayList<>() : tDataSourceBatches;
    }

    @Override
    public List<TDataSourceBatch> selectAll() {
        return dataSourceBatchMapper.selectAll();
    }

    @Override
    public int update(TDataSourceBatch dataSourceBatch) {
        return dataSourceBatchMapper.update(dataSourceBatch);
    }

    @Override
    public int deleteById(Integer id) {
        return dataSourceBatchMapper.deleteById(id);
    }
}
